package com.hengxinguotong.hxgtwygsp.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hengxinguotong.hxgtwygsp.R;
import com.hengxinguotong.hxgtwygsp.model.Year;
import com.hengxinguotong.hxgtwygsp.ui.base.BaseViewHolder;

import java.util.List;

import butterknife.BindView;

/**
 * TODO
 *
 * @author hjz
 * @version V1.0.0
 * @since 2019/4/9 10:52
 */

public class YearPW extends DropListPW<Year>
{
    private OnClickCallback<Year> onClickCallback;
    private List<Year> yearList;
    private YearAdapter yearAdapter;

    public YearPW(Context context, int width)
    {
        super(context, width);
    }

    public void setOnClickCallback(OnClickCallback<Year> onClickCallback)
    {
        this.onClickCallback = onClickCallback;
    }

    @Override
    public void initData(List<Year> list, int index)
    {
        this.yearList = list;
        yearAdapter = new YearAdapter();
        mRecyclerView.setAdapter(yearAdapter);
    }

    public class YearHolder extends BaseViewHolder<Year>
    {
        @BindView(R.id.year_text)
        TextView yearText;

        public YearHolder(View itemView)
        {
            super(itemView);
            itemView.setOnClickListener(yearAdapter);
        }

        @Override
        public void bindData(Year year)
        {
            yearText.setText(year.getName());
            itemView.setTag(year);
        }
    }

    public class YearAdapter extends RecyclerView.Adapter<YearHolder> implements View.OnClickListener
    {

        @NonNull
        @Override
        public YearHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
        {
            View view = View.inflate(context, R.layout.item_year_pw, null);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return new YearHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull YearHolder holder, int position)
        {
            holder.bindData(yearList.get(position));
        }

        @Override
        public int getItemCount()
        {
            return yearList.size();
        }

        @Override
        public void onClick(View v)
        {
            if (onClickCallback != null)
            {
                onClickCallback.onClickPW((Year) v.getTag());
            }
            popupWindow.dismiss();
        }
    }
}
